<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <input type="text" placeholder="说点什么" v-model="weibo.content">
    <!--action 设置上传地址
        name 设置上传参数的名称
        limit 设置上传图片的数量-->
    <el-upload
            action="/upload"
            list-type="picture-card"
            name="pic"
            limit="3"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <hr>
    <input type="button" value="发布微博" @click="insert()">
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo: {
                    url: "",
                    content: ""
                }
            }
        },
        methods: {
            //当删除页面中图片时出发的方法
            //发送请求删除服务器中的图片
            //file代表当前删除的图片文件
            //file.response代表上传该文件成功时服务端响应的内容(当前删除图片路径)
            handleRemove(file, fileList) {
                console.log(file, fileList);
                axios.get("/remove?url=" + file.response).then(function () {
                    console.log("删除服务器图片完成")
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response, file, fileList) {
                //response代表服务器上传成功是响应的数据
                //file代表当前上传成功的文件
                //fileList代表上传成功的所有文件(数组)
                v.weibo.url = response;
            },
            insert() {
                if (v.weibo.url == "") {
                    alert("请选择上传的图片");
                    return;
                }
                axios.post("/insert", v.weibo).then(function (response) {
                    alert("发送完成");
                    location.href = "/";
                })
            }
        }
    })
</script>
</html>